using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class DoanhThuTheoLoaiPhongRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static DoanhThuTheoLoaiPhongDTO CreateDoanhThuTheoLoaiPhongDTOFromDoanhThuTheoLoaiPhongEF(DoanhThuTheoLoaiPhong objEf) // EF -> DTO
        {
            DoanhThuTheoLoaiPhongDTO objDTO = new DoanhThuTheoLoaiPhongDTO();
            Mapper.CreateMap<DoanhThuTheoLoaiPhong, DoanhThuTheoLoaiPhongDTO>();
            objDTO = Mapper.Map<DoanhThuTheoLoaiPhong, DoanhThuTheoLoaiPhongDTO>(objEf);
            return objDTO;
        }

        public static DoanhThuTheoLoaiPhong CreateDoanhThuTheoLoaiPhongEFFromDoanhThuTheoLoaiPhongDTO(DoanhThuTheoLoaiPhongDTO objDTO)    //DTO -> EF
        {
            DoanhThuTheoLoaiPhong objEF = new DoanhThuTheoLoaiPhong();
            Mapper.CreateMap<DoanhThuTheoLoaiPhongDTO, DoanhThuTheoLoaiPhong>();
            objEF = Mapper.Map<DoanhThuTheoLoaiPhongDTO, DoanhThuTheoLoaiPhong>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.DoanhThuTheoLoaiPhongList
                   select x.MaDoanhThu).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.DoanhThuTheoLoaiPhongList
                        orderby x.MaDoanhThu descending
                        select x.MaDoanhThu;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(DoanhThuTheoLoaiPhongDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                DoanhThuTheoLoaiPhong objEF = CreateDoanhThuTheoLoaiPhongEFFromDoanhThuTheoLoaiPhongDTO(objDTO);
                context.AddToDoanhThuTheoLoaiPhongList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(DoanhThuTheoLoaiPhongDTO objDTO)
        {
            try
            {
                DoanhThuTheoLoaiPhong objEF = CreateDoanhThuTheoLoaiPhongEFFromDoanhThuTheoLoaiPhongDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.DoanhThuTheoLoaiPhongList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<DoanhThuTheoLoaiPhongDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.DoanhThuTheoLoaiPhongList
                        orderby x.MaDoanhThu
                        select x).Skip(Start - 1).Take(Record);
            List<DoanhThuTheoLoaiPhongDTO> lst = new List<DoanhThuTheoLoaiPhongDTO>();
            foreach (var obj in query)
            {
                DoanhThuTheoLoaiPhongDTO lp = DoanhThuTheoLoaiPhongRepository.CreateDoanhThuTheoLoaiPhongDTOFromDoanhThuTheoLoaiPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<DoanhThuTheoLoaiPhongDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<DoanhThuTheoLoaiPhongDTO> lst = new List<DoanhThuTheoLoaiPhongDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from DoanhThuTheoLoaiPhong in context.DoanhThuTheoLoaiPhongList
                        select DoanhThuTheoLoaiPhong;
            foreach (var objEf in query)
            {
                DoanhThuTheoLoaiPhongDTO objDTO = CreateDoanhThuTheoLoaiPhongDTOFromDoanhThuTheoLoaiPhongEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public DoanhThuTheoLoaiPhongDTO LayTheoMa(int MaDoanhThuTheoLoaiPhong)
        {
            DoanhThuTheoLoaiPhongDTO objDTO = new DoanhThuTheoLoaiPhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from DoanhThuTheoLoaiPhong in context.DoanhThuTheoLoaiPhongList
                            where DoanhThuTheoLoaiPhong.MaDoanhThu == MaDoanhThuTheoLoaiPhong
                            select DoanhThuTheoLoaiPhong;
                DoanhThuTheoLoaiPhong objEF = query.First();
                objDTO = CreateDoanhThuTheoLoaiPhongDTOFromDoanhThuTheoLoaiPhongEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }
        public bool Xoa(int MaDoanhThuTheoLoaiPhong, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       ChiTietDoanhThuTheoLoaiPhongRepository ChiTietDoanhThuTheoLoaiPhongRepo = new ChiTietDoanhThuTheoLoaiPhongRepository();
                       ChiTietDoanhThuTheoLoaiPhongRepo.XoaTheoMaDoanhThuTheoLoaiPhong(MaDoanhThuTheoLoaiPhong, XoaKhoaNgoai);

                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.DoanhThuTheoLoaiPhongList
                            where x.MaDoanhThu == MaDoanhThuTheoLoaiPhong 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

